package com.chance.cc.crawler.development.scripts.du;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerRecordContext;
import com.chance.cc.crawler.core.downloader.*;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.pipeline.result.CrawlerData;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.core.script.crawler.CrawlerCommonScript;
import com.chance.cc.crawler.core.selector.Json;
import com.chance.cc.crawler.core.tags.CategoryTag;
import com.chance.cc.crawler.core.tags.KVTag;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainUrls;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Field_Domain_Result_Json;
import static org.codehaus.groovy.runtime.EncodingGroovyMethods.md5;

/**
 * @author bx
 * @date 2020/12/6 0006 12:52
 */
public class DUProductNewCrawlerScript extends CrawlerCommonScript {

    private static Logger logger = LoggerFactory.getLogger(DUProductNewCrawlerScript.class);

    //start url
    public static final String startUrl = "du_app/product/detail/crawler/start";

    //商品明细url
    public static final String productDetailUrl = "https://app.dewu.com/api/v1/h5/index/fire/flow/product/detail\\S*";
//    public static final String sellProductlUrl = "https://app.dewu.com/api/v1/h5/inventory/price/h5/queryBuyNowInfo";
//    public static final String askProductlUrl = "https://app.dewu.com/api/v1/h5/newbidding/buyer/view";

    public static final String site = "product_detail_new";
    public static final String site_biz = "product_detail_new";
    public static final String crawler_type = "crawler_type";
    public static final String tagBrandName = "brand_name";
    public static final String tagBrandId = "brand_id";
    public static final String tagProductId = "product_id";
    public static final String tagProductDetail = "product_detail";
    public static final String tagSellProduct = "sell_product";
    public static final String tagAskProduct = "ask_product";

    private static LinkedBlockingQueue<String> userAgents = new LinkedBlockingQueue<>();
    public static Object uaObject = new Object();

    //下载器
    private Downloader httpDownload;
    private static final Object httpDownloadObj = new Object();

    //ip proxy obtain url 随机显示 明细信息，当天去重
    public static final String ip_proxy_obtain_url = "http://15301929770.v4.dailiyun.com/query.txt?key=NPDD5F9792" +
            "&word=&count=%d&rand=true&ltime=0&norepeat=true&detail=true";

    //ip proxy queue
    public LinkedBlockingQueue<ProxyBean> proxyBeans = null;
    private static final Object initProxyObj = new Object();
    private static int initCount = 10;
    private static final String username = "15301929770";
    private static final String password = "chancedata123";

    @Override
    public List<CrawlerRecord> prepareRequest(CrawlerRequestRecord requestRecord, List<CrawlerRequestRecord> supportSourceRecords) {
        List<CrawlerRecord> allItemRecords = new ArrayList<>();

        if(userAgents.size() <1){
            synchronized (uaObject){
                if (userAgents.size() <1){
                    try {
                        userAgents.addAll(IOUtils.readLines(new FileInputStream("/data/chance_crawler_runner/data/ua/ua_du_app.txt"), "utf-8"));
                    } catch (IOException e) {
                        logger.error(e.getMessage());
                    }
                }
            }
        }

        HttpRequest httpRequest = requestRecord.getHttpRequest();
        httpRequest.addHeader("User-Agent",genRandomUA());
//
//        initProxyQueues(); //初始代理队列
//
//        Proxy proxy = extractProxy();//提取代理
//        if (proxy == null){
//            logger.error("extract proxy is null!");
//        }
//
//        requestRecord.getHttpConfig().setProxy(proxy);//设置代理
//        requestRecord.getHttpConfig().setUseProxy(true);

        requestRecord.setNeedWashPage(true);

        if (requestRecord.tagsCreator().bizTags().hasKVTag(Tag_Field_Domain_Result_Json)) {
            KVTag domainResultJson = requestRecord.tagsCreator().bizTags().getCategoryTag().getKVTag(Tag_Field_Domain_Result_Json);
            CrawlerDomainUrls crawlerDomainUrls = JSON.parseObject(String.valueOf(domainResultJson.getVal()), CrawlerDomainUrls.class);
            String url = crawlerDomainUrls.getUrl();
            JSONObject contentObj = JSONObject.parseObject(url);
            requestRecord.setNeedParsedPage(true);
            genInitProductDetailRecord(requestRecord, httpRequest, contentObj);
            logger.info("record: [{}] start",requestRecord.getRecordKey());
        }

        return allItemRecords;
    }

//    private void initProxyQueues(){
//        if(proxyBeans == null){
//            synchronized (initProxyObj) {
//                if (proxyBeans == null){
//                    proxyBeans = new LinkedBlockingQueue<>();
//                    proxyBeans.addAll(genProxyBean(initCount));
//                }
//            }
//        }
//    }
//
//    private Proxy extractProxy(){
//        Proxy proxy = null;
//        try {
//            ProxyBean proxyBean = proxyBeans.poll(5, TimeUnit.SECONDS);
//            if (proxyBean != null){
//                if (System.currentTimeMillis() >= proxyBean.getEndTime()){
//                    logger.info("proxy ip {} is expired! get again...",proxyBean.getProxy().getHost());
//                    List<ProxyBean> proxyBeanList = genProxyBean(1);
//                    if (proxyBeanList.size() == 1){
//                        proxyBean = proxyBeanList.get(0);
//                        proxy = proxyBean.getProxy();
//                        proxyBeans.add(proxyBean); //返还队列
//                    }
//                } else {
//                    proxy = proxyBean.getProxy();
//                    proxyBeans.add(proxyBean); //返还队列
//                }
//            }
//        } catch (InterruptedException e) {
//           logger.error(e.getMessage());
//        }
//        return proxy;
//    }
//
//    private List<ProxyBean> genProxyBean(int genCount){
//        List<ProxyBean> proxyBeanList = new ArrayList<>();
//        HttpRequest httpRequest = new HttpRequest();
//        httpRequest.setUrl(String.format(ip_proxy_obtain_url,genCount));
//        HttpPage httpPage = httpDownload.download(httpRequest, HttpConfig.me("ip_proxy"));
//        if (!httpPage.isDownloadSuccess()){
//            logger.error("proxy ip obtain error: {}",httpPage.getRawText());
//            return proxyBeanList;
//        }
//        try {
//            String responseContent = httpPage.getRawText().trim();
//            String[] items = responseContent.split("\n");
//            for (String item : items) {
//                try {
//                    String[] itemArray = item.split(",");
//                    String host = itemArray[0].split(":")[0];
//                    String port = itemArray[0].split(":")[1];
//                    String addr = itemArray[2];
//                    long startTime = Long.valueOf(itemArray[3])*1000;
//                    long endTime = Long.valueOf(itemArray[4])*1000;
//                    long proxyStartTime = System.currentTimeMillis();
//                    long proxyEndTime = proxyStartTime + startTime+endTime;
//                    ProxyBean proxyBean = new ProxyBean(host, Integer.valueOf(port), username, password);
//                    proxyBean.setAddr(addr).setStartTime(proxyStartTime).setEndTime(proxyEndTime);
//                    proxyBeanList.add(proxyBean);
//                    logger.info("obatin proxy new ip:{},addr {},term of validity:{}",
//                            proxyBean.getProxy().getHost(),proxyBean.getAddr(),(proxyBean.getEndTime() - proxyBean.getStartTime()));
//                } catch (Exception e) {
//                    logger.error("proxy ip response content item parse error:{},item :{}",e.getMessage(),item);
//                }
//            }
//        } catch (Exception e) {
//            logger.error("proxy ip response content parse error:{},content :{}",e.getMessage(),httpPage.getRawText());
//        }
//        return proxyBeanList;
//    }
//
//    @Override
//    public void beforeDownload(CrawlerRecordContext context) {
//        if (httpDownload == null){
//            synchronized (httpDownloadObj) {
//                if (httpDownload == null){
//                    httpDownload = context.getPageDownloader();
//                }
//            }
//        }
//        super.beforeDownload(context);
//    }

    @Override
    public List<CrawlerRequestRecord> parseLinks(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        String requestUrl = crawlerRecord.getHttpRequest().getUrl();
        List<CrawlerRequestRecord> crawlerRequestRecords = new ArrayList<>();

        Proxy proxy = crawlerRecord.getHttpConfig().getProxy();
        if (proxy == null){
            logger.error("ip proxy is null ,will retry",requestUrl);
            addCrawlerRecords(crawlerRequestRecords,crawlerRecord);
            crawlerRecord.setNeedWashPage(false); //下载异常，不需要清洗页面
            return crawlerRequestRecords;
        }

        if(crawlerRecord.isDownload() && !page.isDownloadSuccess()){
            logger.error("du product download url {} proxy has error ,will retry",requestUrl);
            addCrawlerRecords(crawlerRequestRecords,crawlerRecord);
            crawlerRecord.setNeedWashPage(false); //下载异常，不需要清洗页面
            return crawlerRequestRecords;
        }

        parseProductDetailLinks(crawlerRequestRecords,crawlerRecord,page);
        return crawlerRequestRecords;
    }

    @Override
    public List<CrawlerData> washPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        String requestUrl = crawlerRecord.getHttpRequest().getUrl();
        List<CrawlerData> crawlerDatas = new ArrayList<>();
        washProductInfo(crawlerDatas,crawlerRecord,page);

        return crawlerDatas;
    }

    private void parseProductDetailLinks(List<CrawlerRequestRecord> crawlerRequestRecords,CrawlerRequestRecord crawlerRecord, HttpPage page){
        Json shoesJson = new Json(page.getRawText());
        try {
            String code = shoesJson.jsonPath($_type + ".code").get();
            String status = shoesJson.jsonPath($_type + ".status").get();
            if ("20801001".equals(code)){
                crawlerRecord.tagsCreator().bizTags().addCustomKV("is_del","delete");
                crawlerRecord.setNeedWashPage(true);
                return;
            }
            if (!"200".equals(code) || !"200".equals(status)){
                logger.error("du app error ua:{}",page.getRequest().getHeaders().get("User-Agent"));
                throw new Exception("du product detail download error");
            }
            String spuId = shoesJson.jsonPath($_type + ".data.detail.spuId").get();
//            List<String> all = shoesJson.jsonPath("data.image.spuImage.images[*].url").all();
//            for (String img : all) {
//                internalDownloadCommon(crawlerRequestRecords,crawlerRecord,img);
//            }
            crawlerRecord.tagsCreator().bizTags().addCustomKV(tagProductId,spuId);
//            if (!crawlerRecord.isNeedWashPage()){
//                CrawlerRequestRecord crawlerRequestRecord = genSellProductRecords(crawlerRequestRecords,crawlerRecord,spuId);
//            }
        } catch (Exception e) {
            logger.error("will retry [{}],status code {},page rawtext{},error:{}"
                    ,crawlerRecord.getRecordKey(),page.getStatusCode(),page.getRawText(),e.getMessage());
            addCrawlerRecords(crawlerRequestRecords,crawlerRecord);
            crawlerRecord.setNeedWashPage(false);
        }
    }

    private void internalDownloadCommon(List<CrawlerRequestRecord> crawlerRequestRecords,CrawlerRequestRecord crawlerRecord,String url) throws NoSuchAlgorithmException {
        if (StringUtils.isNotBlank(url)){
            CrawlerRequestRecord commentCountRecord = CrawlerRequestRecord.builder()
                    .itemPageRequest(crawlerRecord)
                    .httpUrl(url)
                    .httpHead("appid","wxapp")
                    .httpHead("platform","h5")
                    .httpHead("content-type","application/json")
                    .releaseTime(System.currentTimeMillis())
                    .requestLabelTag(CrawlerEnum.CrawlerRequestType.internalDownload)
                    .build();
            commentCountRecord.getHttpConfig().setResponseTextGenerateHtml(false);
            crawlerRequestRecords.add(commentCountRecord);
        }
    }

    private String genRandomUA(){
        String ua = null;
        try {
            ua = userAgents.poll(3, TimeUnit.SECONDS);
            logger.info("poll new ua:{}",ua);
            userAgents.add(ua);
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }

        if (StringUtils.isBlank(ua)){
            logger.error("extract ua is null, return default ua");
            ua = "Mozilla/5.0 (Linux; Android 8.0.0; MI 5s Build/OPR1.170623.032; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2885 MMWEBSDK/20210601 Mobile Safari/537.36 MMWEBID/6446 MicroMessenger/8.0.7.1920(0x28000753) Process/appbrand0 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android";
        }
        return ua;
    }

    private void washProductInfo(List<CrawlerData> crawlerDatas,CrawlerRequestRecord crawlerRecord, HttpPage page){
        try {
            CategoryTag categoryTag = crawlerRecord.tagsCreator().bizTags().getCategoryTag();
            String spuId = categoryTag.getKVTagStrVal(tagProductId);
            String crawlerType = categoryTag.getKVTagStrVal(crawler_type);

            String productUrl = "http://m.poizon.com/router/product/ProductDetail?spuId=" + spuId + "&sourceName=shareDetail";
            //根据详情结果判断商品是否已经下架
            Json shoesJson = new Json(page.getRawText());
            String code = shoesJson.jsonPath($_type + ".code").get();
            String msg = shoesJson.jsonPath($_type + ".msg").get();

            String cateId = categoryTag.getKVTagStrVal("cateId");
            String category = categoryTag.getKVTagStrVal("category");
            String brandName = categoryTag.getKVTagStrVal(tagBrandName);
            String brandId = categoryTag.getKVTagStrVal(tagBrandId);
            JSONObject result = new JSONObject();
            result.put("spuId",spuId);
            result.put("category",category);
            result.put("cateId",cateId);
            result.put("brandName",brandName);
            result.put("brandId",brandId);

            String isDel = crawlerRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("is_del");
            if (StringUtils.isNotBlank(isDel) && "delete".equals(isDel)){
                logger.error("du product {} detail is not exist",productUrl);
            }
            CrawlerData crawlerData = CrawlerData.builder()
                    .data(crawlerRecord, page)
                    .dataId(StringUtils.joinWith("-", domain(),site, CrawlerEnum.CrawlerDataType.article.enumVal(), spuId))
                    .url(productUrl)
                    .releaseTime(System.currentTimeMillis())
                    .addContentKV(tagProductDetail,page.getRawText())
                    .requestLabelTag(CrawlerEnum.CrawlerRequestType.result)
                    .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                    .flowInPipelineTag("kafka_data_result")
                    .flowInPipelineTag("file_data_result")
    //                        .flowInPipelineTag("console")
                    .build();
            crawlerData.setFilterPipelineResult(true);
            crawlerData.tagsCreator().bizTags().addDomain(domain());
            crawlerData.tagsCreator().bizTags().addSite(site);
            crawlerData.tagsCreator().bizTags().addSiteBiz(site_biz);
            crawlerDatas.add(crawlerData);
        } catch (Exception e) {
            logger.error("wash product detail error {}, page rawtext {}",e.getMessage(),page.getRawText());
        }

    }

    @Override
    public String domain() {
        return "du_app";
    }

    @Override
    public void initUrlRegulars() {
        addUrlRegular(startUrl);
        addUrlRegular(productDetailUrl);
//        addUrlRegular(sellProductlUrl);
//        addUrlRegular(askProductlUrl);
    }

    @Override
    public boolean crawlerCheck(CrawlerRequestRecord crawlerRecord) {
        if ("https://m.dewu.com/sns/v1/content/posts-note-list"
                .equals(crawlerRecord.getHttpRequest().getUrl())){
            return false;
        }
        CategoryTag categoryTag = crawlerRecord.tagsCreator().bizTags().getCategoryTag();
        String crawlerSite = categoryTag.getKVTagStrVal("site");
        return crawlerSite.equalsIgnoreCase(site);
    }

    @Override
    public void afterExecute(CrawlerRecordContext context) {

    }

    private void genInitProductDetailRecord(CrawlerRequestRecord requestRecord, HttpRequest httpRequest, JSONObject contentObj) {
        String spuId = contentObj.getString("spuId");
        String cateId = contentObj.getString("cateId");
        String category = contentObj.getString("category");
        String brandId = contentObj.getString("brandId");
        String brandName = contentObj.getString("brandName");
        httpRequest.setMethod(HttpConstant.Method.POST);
        requestRecord.setRecordKey(StringUtils.joinWith("-",domain(),site, CrawlerEnum.CrawlerDataType.article.enumVal(),spuId));
        requestRecord.setReleaseTime(System.currentTimeMillis());
        Map<String,Object> params = new HashMap<>();
        params.put("spuId",spuId);
        params.put("productSourceName","");
        params.put("propertyValueId","0");

        String sign = genSignature(params);
        params.put("sign",sign);
        HttpRequestBody httpRequestBody = HttpRequestBody.json(JSON.toJSONString(params), "utf-8");
        httpRequest.setRequestBody(httpRequestBody);

        httpRequest.addHeader("Host","app.dewu.com");
        httpRequest.addHeader("Connection","keep-alive");
        httpRequest.addHeader("Content-Type","application/json");
        httpRequest.addHeader("platform","h5");
        httpRequest.addHeader("AppId","wxapp");
        httpRequest.addHeader("appVersion","4.4.0");
        httpRequest.addHeader("charset","utf-8");
        httpRequest.addHeader("Accept-Encoding","gzip,compress,br,deflate");
        httpRequest.addHeader("Referer","https://servicewechat.com/wx3c12cdd0ae8b1a7b/225/page-frame.html");
        httpRequest.addHeader("wxapp-login-token","");
        requestRecord.tagsCreator().bizTags().getCategoryTag().getKvTags().remove(Tag_Field_Domain_Result_Json); //移除
        requestRecord.tagsCreator().requestTags().removeRequestType(CrawlerEnum.CrawlerRequestType.filter);
        requestRecord.setRecordKey(StringUtils.joinWith("-",domain(),site,spuId));
        requestRecord.tagsCreator().bizTags().addCustomKV(crawler_type,"productDetail");
        requestRecord.tagsCreator().bizTags().addCustomKV(tagProductId,spuId);
        requestRecord.tagsCreator().bizTags().addCustomKV("cateId",cateId);
        requestRecord.tagsCreator().bizTags().addCustomKV("category",category);
        requestRecord.tagsCreator().bizTags().addCustomKV(tagBrandId,brandId);
        requestRecord.tagsCreator().bizTags().addCustomKV(tagBrandName,brandName);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

    }


    public static String genSignature(Map<String,Object> params) {
        return genSignature(params,"19bc545a393a25177083d4a748807cc0");
    }

    public static String genSignature(Map<String,Object> params,String salt) {
        if(params == null){
            return null;
        }
        String sign = "";
        StringBuffer sb = new StringBuffer();
        try {
            // 1. 字典升序排序
            SortedMap<String,Object> sortedMap = new TreeMap<>(params);
            // 2. 拼按URL键值对
            Set<String> keySet = sortedMap.keySet();
            for(String key : keySet){
                //sign不参与算法
                if(key.equals("sign")){
                    continue;
                }

                Object obj =  sortedMap.get(key);
                String value = obj.toString() ;
                if (obj instanceof Map){
                    value = JSON.toJSONString(obj);
                }
                if (obj instanceof List){
                    List list = (List) obj;
                    value = "";
                    for (Object o : list) {
                        if (value.length() > 0){
                            value += ",";
                        }
                        value += JSON.toJSONString(o);
                    }
//                    value = ","+JSON.toJSONString(obj);
                }
//                if ("[]".equals(value)){
//                    value = "";
//                }
                sb.append(key).append(value);

            }
            String uriString = sb.toString() + salt;
            // 3. MD5运算得到请求签名
            sign = md5(uriString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }


    private void addCrawlerRecords(List<CrawlerRequestRecord> crawlerRequestRecords,CrawlerRequestRecord crawlerRecord){

//        int count = 1;
//        CrawlerBusinessTags crawlerBusinessTags = crawlerRecord.tagsCreator().bizTags();
//        if (crawlerBusinessTags.hasKVTag("download_retry_count")){
//            count = (int) crawlerBusinessTags.getCategoryTag().getKVTag("download_retry_count").getVal();
//            if (count >= 20){
//                logger.error("du product info download he number of retries exceeds the limit" +
//                        ",request url {},download detail {}",crawlerRecord.getHttpRequest().getUrl());
//                return;
//            }
//        }
//
//        count++;
//        crawlerBusinessTags.addCustomKV("download_retry_count",count);

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .turnPageRequest(crawlerRecord)
                .httpRequest(crawlerRecord.getHttpRequest())
                .releaseTime(System.currentTimeMillis())
                .copyBizTags()
                .build();
        crawlerRequestRecord.tagsCreator().requestTags().removeRequestType(CrawlerEnum.CrawlerRequestType.filter);
        crawlerRequestRecord.setNeedWashPage(crawlerRecord.isNeedWashPage());
//        logger.error("record [{}] will to reparse",JSONObject.toJSONString(crawlerRequestRecord));
        crawlerRequestRecords.add(crawlerRequestRecord);
    }

    public long dateToTimestamp(String dataStr) throws ParseException {
        String regEx="[^0-9]+"; //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
        Pattern pattern = Pattern.compile(regEx);

        if (dataStr.equals("刚刚")){
            return System.currentTimeMillis();
        } else if (Pattern.matches("\\d*秒前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*1000L);
        } else if (Pattern.matches("\\d*分钟前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*60*1000L);
        } else if (Pattern.matches("\\d*小时前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*60*60*1000L);
        } else if (Pattern.matches("\\d*小时\\d*分钟前",dataStr)){
            int hourNumber = Integer.valueOf(pattern.split(dataStr)[0]);
            int secondNumber = Integer.valueOf(pattern.split(dataStr)[1]);
            long totalMillis = hourNumber*60*60*1000L + secondNumber*60*1000L;
            return (System.currentTimeMillis() - totalMillis);
        } else if (Pattern.matches("\\d*天前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*24*60*60*1000L);
        } else if (dataStr.startsWith("今天")){
            String currentTime = DateFormatUtils.format(System.currentTimeMillis(), TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("今天", currentTime);
        } else if (dataStr.startsWith("昨天")){
            String yesterdayTime = DateFormatUtils.format(System.currentTimeMillis() - 60*60*24*1000L, TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("昨天", yesterdayTime);
        } else if (dataStr.startsWith("前天")){
            String beforeYesterdayTime = DateFormatUtils.format(System.currentTimeMillis() - 2*60*60*24*1000L, TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("前天", beforeYesterdayTime);
        } else if (Pattern.matches("\\d*月\\d*[日]*", dataStr)){
            Calendar cal = Calendar.getInstance();
            dataStr = cal.get(Calendar.YEAR)+"年"+dataStr;
        } else if (Pattern.matches("\\d*-\\d*", dataStr)){
            Calendar cal = Calendar.getInstance();
            dataStr = cal.get(Calendar.YEAR)+"-"+dataStr;
        }
        return DateUtils.parseDateStrictly(dataStr, TimeForamtEnum.allFormats()).getTime();
    }

    public enum TimeForamtEnum {
        format1("yyyy年MM月dd日"),
        format1_1("yyyy年MM月dd日 HH:mm:ss"),
        format1_2("yyyy年MM月dd日 HH:mm"),
        format1_3("yyyy年MM月dd日HH:mm"),
        format1_4("yyyy年MM月dd日 HH点mm分"),
        format1_5("yyyy年MM月dd日 HH点mm分ss秒"),
        format1_6("yyyy年MM月dd日HH点mm分"),
        format1_7("yyyy年MM月dd日HH点mm分ss秒"),


        format4("yyyy-MM-dd HH:mm:ss"),
        format5("yyyy-MM-dd HH:mm"),
        format6("yyyy-MM-dd"),

        format7("yyyy/MM/dd HH:mm:ss"),
        format7_1("MM/dd/yyyy HH:mm:ss"),
        format8("yyyy/MM/dd HH:mm"),
        format9("yyyy/MM/dd"),

        format10("yyyy.MM.dd HH:mm:ss"),
        format11("yyyy.MM.dd HH:mm"),
        format12("yyyy.MM.dd"),

        format13("EEE MMM d HH:mm:ss +0800 yyyy");

        private String format;

        private TimeForamtEnum(String format) {
            this.format = format;
        }

        public static String[] allFormats() {
            TimeForamtEnum[] timeForamtEnums = TimeForamtEnum.values();
            String[] formats = new String[timeForamtEnums.length];
            for (int count = 0; count < timeForamtEnums.length; count++) {
                formats[count] = timeForamtEnums[count].format;
            }
            return formats;
        }

        public String getFormat() {
            return format;
        }
    }


    class ProxyBean{
        private Proxy proxy;
        private long startTime;
        private long endTime;
        private String addr;

        public ProxyBean(String ip,int port,String user,String psssword) {
            Proxy proxy = new Proxy();
            proxy.setHost(ip);
            proxy.setPort(port);
            proxy.setUsername(user);
            proxy.setPassword(psssword);
            this.proxy = proxy;
        }

        public Proxy getProxy() {
            return proxy;
        }

        public long getStartTime() {
            return startTime;
        }

        public ProxyBean setStartTime(long startTime) {
            this.startTime = startTime;
            return this;
        }

        public long getEndTime() {
            return endTime;
        }

        public ProxyBean setEndTime(long endTime) {
            this.endTime = endTime;
            return this;
        }

        public String getAddr() {
            return addr;
        }

        public ProxyBean setAddr(String addr) {
            this.addr = addr;
            return this;
        }
    }
}
